package www.cskaoyan.com.controller;

import www.cskaoyan.com.bean.Dormitory;
import www.cskaoyan.com.service.DormitoryManagerService;
import www.cskaoyan.com.service.impl.DormitoryManagerServiceImpl;
import www.cskaoyan.com.utils.Request;
import www.cskaoyan.com.utils.Response;

import java.sql.SQLException;
import java.util.List;

public class DormitoryManagerController {

    // 调用逻辑层
    DormitoryManagerService dormitoryManagerService;
    public DormitoryManagerController(){
        this.dormitoryManagerService = new DormitoryManagerServiceImpl();
    }

    /**
     * 获取宿舍列表
     * @param request:
     *  @param request:
     * @return Response(200, "成功", true)
     */
    public Response getDormitoryList(Request request)  {
        System.out.println("/DormitoryManager/getDormitoryList");

        Response response;
        List<Dormitory> list = null;
        try {
            list =  dormitoryManagerService.getDormitoryList();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (list != null  && list.size() > 0){
            // 存在
            response = new Response(200, "宿舍列表", true);
            response.setData(list);
        }else {
            // 不存在
            response = new Response(0, "没有宿舍列表", true);
        }
        return  response;
    }


    /**
     * 创建宿舍
     * @param request:Request{url='DormitoryManager/createDormitory', methods='POST', parameters={address=C8, dormitoryName=1103}}
     * @param request: parameters={address=C8, dormitoryName=1103}
     * @return Response(200, "成功", true)
     */
    public Response createDormitory(Request request)  {
        System.out.println("/DormitoryManager/createDormitory");
        System.out.println(request);
        Response response;
        Dormitory dormitory = new Dormitory(request.getParameters());
        Integer classId = null;
        try {
            dormitoryManagerService.createDormitory(dormitory);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        response = new Response(200, "创建成功", true);
        return  response;
    }

    /**
     * 删除班级
     * @param request:  Request{url='ClassManager/deleteClass', methods='Get', parameters={id=2}}
     *  @param request:  parameters={id=2}
     * @return Response(200, "成功", true)
     */
    public Response deleteDormitory(Request request)  {
        System.out.println("/DormitoryManager/deleteDormitory");
        System.out.println(request);
        Response response;
        Dormitory dormitory = new Dormitory(request.getParameters());
        try {
            dormitoryManagerService.deleteDormitory(dormitory);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response = new Response(200, "删除成功", true);
        return  response;
    }

    /**
     * 修改宿舍信息
     * @param request:Request{url='DormitoryManager/createDormitory', methods='POST', parameters={address=C8, dormitoryName=1103}}
     * @param request: parameters={id=1, address=C8, dormitoryName=1103}
     * @return Response(200, "成功", true)
     */
    public Response changeDormitory(Request request)  {
        System.out.println("/DormitoryManager/changeDormitory");
        System.out.println(request);
        Response response;
        Dormitory dormitory = new Dormitory(request.getParameters());
        try {
            dormitoryManagerService.changeDormitory(dormitory);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response = new Response(200, "修改成功", true);
        return  response;
    }
}
